package com.yaozheng.vocationaltraining.fragment.exam;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.PoWoDictionaryActivity;
import com.yaozheng.vocationaltraining.adapter.MyExamItemAdapter;
import com.yaozheng.vocationaltraining.adapter.PKMorePersonPlayerStatusAdapter;
import com.yaozheng.vocationaltraining.adapter.PoWoDictionaryItemAdapter;
import com.yaozheng.vocationaltraining.bean.CategoriesBean;
import com.yaozheng.vocationaltraining.bean.CategoryItemBean;
import com.yaozheng.vocationaltraining.bean.MyExamCategoriesBean;
import com.yaozheng.vocationaltraining.bean.MyExamItemBean;
import com.yaozheng.vocationaltraining.fragment.BaseFragment;
import com.yaozheng.vocationaltraining.iview.ILoadMoreView;
import com.yaozheng.vocationaltraining.iview.exam.IMyExamView;
import com.yaozheng.vocationaltraining.manage.ListViewLoadMoreManage;
import com.yaozheng.vocationaltraining.service.PoWoDictionaryService;
import com.yaozheng.vocationaltraining.service.exam.MyExamService;
import com.yaozheng.vocationaltraining.service.impl.PoWoDictionaryServiceImpl;
import com.yaozheng.vocationaltraining.service.impl.exam.MyExamServiceImpl;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by moon on 2017/11/20.
 */

@EFragment(R.layout.activity_my_exam)
public class MyExamFragment extends BaseFragment
        implements SwipeRefreshLayout.OnRefreshListener, ILoadMoreView, IMyExamView {

    @ViewById
    TextView headTitleText;

    @ViewById
    ListView listView;
    List<MyExamItemBean> myExamItemData = new ArrayList<>();
    MyExamItemAdapter myExamAdapter;


    @ViewById
    LinearLayout type_contain_layout;
    List<MyExamCategoriesBean> list = new ArrayList<>();

    int categoriyId = 1;
    int typeId = 0;

    int lastId;

    LayoutInflater inflater;


    /**
     * 下拉刷新
     */
    @ViewById
    SwipeRefreshLayout swipeRefreshLayout;

    ListViewLoadMoreManage listViewLoadMoreManage;

    @Bean(MyExamServiceImpl.class)
    MyExamService myExamService;

    @AfterViews
    public void initView() {
        headTitleText.setText("我的考试");
        myExamService.init(this);
        inflater = this.getActivity().getLayoutInflater();


        myExamAdapter = new MyExamItemAdapter(this.getActivity()
                ,myExamItemData);
        listView.setAdapter(myExamAdapter);
//

        loadCategoriesData();
        
//
       // onRefresh();
        swipeRefreshLayout.setOnRefreshListener(this);
        listViewLoadMoreManage = new ListViewLoadMoreManage(this,
                getBaseActivity(), listView);
    }

    private void loadCategoriesData() {
        myExamService.loadCategories();
    }


    @UiThread
    @Override
    public void responseSuccess(JSONObject jsonObject) {
        try {
            cancelLoadDataProgressDialog();
            JSONArray dataList = TypeUtils.getJsonArray(jsonObject, "data","exams");
            if (dataList != null) {
                if (lastId == 0) {
                    myExamItemData.clear();
                }

                for (int i = 0; i < dataList.size(); i++) {
                    JSONObject jsonObject2 = dataList.getJSONObject(i);

                    MyExamItemBean bean = new MyExamItemBean();
                    bean.setUserId(jsonObject2.getInt("userId"));
                    bean.setSubject(jsonObject2.getString("subject"));
                    bean.setScore(jsonObject2.getInt("score"));
                    bean.setStartTime(jsonObject2.getString("startTime"));
                    bean.setPaperId(jsonObject2.getInt("paperId"));

                    myExamItemData.add(bean);
                }

                myExamAdapter.setData(myExamItemData);
                myExamAdapter.notifyDataSetChanged();

            }
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
        }
    }

    @UiThread
    @Override
    public void responseError(String error) {

    }

    @Override
    public void cancelLoadDataProgressDialog() {
        swipeRefreshLayout.setRefreshing(false);
        listViewLoadMoreManage.loadMoreComplete();
    }

    @Override
    public void onRefresh() {
        lastId = 0;
        myExamService.list(typeId,lastId);
    }

    @Override
    public void loadMoreData() {
        myExamService.list(typeId,lastId);
    }

    @UiThread
    @Override
    public void categoriesListSuccess(JSONObject jsonObject) {
        JSONArray categoriesList = TypeUtils.getJsonArray(jsonObject,"data",
                "categories");

        for (int i = 0; i < categoriesList.size(); i++) {
            JSONObject jsonObject2 = categoriesList.getJSONObject(i);

            MyExamCategoriesBean bean = new MyExamCategoriesBean();
            bean.setId(jsonObject2.getInt("id"));
            bean.setActiveIcon(jsonObject2.getString("activeIcon"));
            bean.setIcon(jsonObject2.getString("icon"));
            bean.setName(jsonObject2.getString("name"));
            bean.setTypeId(jsonObject2.getInt("typeId"));
            list.add(bean);
        }

        type_contain_layout.removeAllViews();
        if(list != null && list.size() > 0){
                    for(int i= 0 ; i < list.size() ; i++){
                        MyExamCategoriesBean bean = list.get(i);
                        View dictionaryItemLayout = inflater.inflate(R.layout.dictionary_item
                                , null);
                        dictionaryItemLayout.setTag(bean.getId());
                        ImageView iconImageView = (ImageView) dictionaryItemLayout.findViewById(R.id.icon_imageview);
                        TextView nameTextView = (TextView) dictionaryItemLayout.findViewById(R.id.name_textview);

                        nameTextView.setText(bean.getName());


                        if(categoriyId == bean.getId()){
                            typeId = bean.getTypeId();
                            Glide.with(MyExamFragment.this.getActivity().getApplicationContext())
                                    .load(bean.getActiveIcon())
                                    .crossFade()
                                    .into(iconImageView);
                            nameTextView.setTextColor(MyExamFragment.this.getActivity().getResources().getColor(R.color.footer_bg_color));
                        }else{
                            Glide.with(MyExamFragment.this.getActivity().getApplicationContext())
                                    .load(bean.getIcon())
                                    .crossFade()
                                    .into(iconImageView);
                            nameTextView.setTextColor(Color.BLACK);
                        }

                        dictionaryItemLayout.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                categoriyId = (int) view.getTag();
                                for(int i = 0 ; i < type_contain_layout.getChildCount() ; i++){
                                    View childView = type_contain_layout.getChildAt(i);
                                    if(childView.getTag().toString().equals(view.getTag().toString())){
                                        typeId = list.get(i).getTypeId();
                                        Glide.with(MyExamFragment.this.getActivity().getApplicationContext())
                                                .load(list.get(i).getActiveIcon())
                                                .crossFade()
                                                .into((ImageView) childView.findViewById(R.id.icon_imageview));
                                        ((TextView) childView.findViewById(R.id.name_textview)).setTextColor(MyExamFragment.this.getActivity().getResources().getColor(R.color.footer_bg_color));
                                    }else{
                                        Glide.with(MyExamFragment.this.getActivity().getApplicationContext())
                                                .load(list.get(i).getIcon())
                                                .crossFade()
                                                .into((ImageView) childView.findViewById(R.id.icon_imageview));
                                        ((TextView) childView.findViewById(R.id.name_textview)).setTextColor(Color.BLACK);
                                    }
                                }

                                onRefresh();
                            }
                        });
                        type_contain_layout.addView(dictionaryItemLayout);
                    }
        }


        onRefresh();
    }

    @UiThread
    @Override
    public void categoriesListError(String error) {

    }
}
